﻿using Microsoft.AspNetCore.Mvc;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Models;
using StudentManagement.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StudentManagement.Controllers
{
    public class TreeModel
    {
        public String Id { get; set; }
        public string Text { get; set; }
        public string Parent { get; set; }
    }

    public class MenuController : Controller
    {
        private IRepository<Menu> repo;

        public MenuController(IRepository<Menu> menuRepo)
        {
            this.repo = menuRepo;
        }

        public IActionResult Index()
        {
            var model = repo.GetAllAsList();
            return View(model);
        }

        /// <summary>
        /// 获取menu的数据，以json格式返回
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuTreeData()
        {
            var menus = repo.GetAllAsList();            
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus) {
                treeModels.Add(
                    new TreeModel() { 
                        Id = menu.Id.ToString(), 
                        Text = menu.Name, 
                        Parent = menu.ParentId == 0 ? "#" : menu.ParentId.ToString() 
                    });
            }
            return Json(treeModels);
        }

        /// <summary>
        /// 根据parentId获取所有的menu
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public IActionResult GetMenusByParent(int parentId)
        {
            return Json(repo.GetAll().Where(m=>m.ParentId ==parentId).ToList());
        }

        public ActionResult Get(int id)
        {
            return Json(repo.Get(id));
        }

        [HttpPost]
        public ActionResult Edit(Menu menu)
        {
            if (!ModelState.IsValid) {
                return Json(new
                {
                    result = "Failed",
                    message = "保存失败！"
                });
            }
            repo.Update(menu);
            return Json(new {
                result = "Success",
                message = "Success"
            });
        }

        [HttpPost]
        public ActionResult Create(Menu menu)
        {
            repo.Insert(menu);
            return Json(new
            {
                result = "Success",
                message = "Success"
            });
        }

        [HttpPost]
        public IActionResult Remove(int id)
        {
            var item = repo.Get(id);
            if (item != null) {

                repo.Delete(item);
            }

            return RedirectToAction("Index", this);
        }

        public ActionResult GetTableRows(int parentId)
        {
            var data = repo.GetAll().Where(m => m.ParentId == parentId).ToList();
            return Json(AjaxHelper.RazorViewToString(this, "tablerows", data));
        }
    }
}
